<template>
	<view class="add-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="出差审批"></uni-navbar></view>

		<u-tabs class="uTabs" :list="tabsList" :is-scroll="false" :current="tabsCurrent" @change="tabsChange"></u-tabs>

		<view v-show="tabsCurrent == 0">
			<view class="form-box">
				<u-form :model="form" ref="uForm">
					<u-form-item class="form-item" required label="部门" labelAlign="left" label-width="190">
						<view>{{ dept }}</view>
					</u-form-item>
					<u-form-item class="form-item" required label="职务" labelAlign="left" label-width="190" prop="userJob">
						<u-input v-model="form.userJob" type="text" placeholder="请输入职务" />
					</u-form-item>
					<u-form-item class="form-item" required label="交通工具" labelAlign="left" label-width="190" prop="evectionType">
						<u-input v-model="kind" type="select" :select-open="kindShow" placeholder="请选择交通工具" @click="kindShow = true" />
						<u-select :list="kindList" v-model="kindShow" @confirm="kindConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" required label="单程往返" labelAlign="left" label-width="190" prop="evectionType">
						<u-input v-model="type" type="select" :select-open="typeShow" placeholder="请选择单程往返" @click="typeShow = true" />
						<u-select :list="typeList" v-model="typeShow" @confirm="typeConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" required label="开始时间" labelAlign="left" label-width="190" prop="beginTime">
						<date-time-picker placeholder="请选择开始时间" :defaultValue="form.beginTime" @change="timeChange($event, 'beginTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" required label="结束时间" labelAlign="left" label-width="190" prop="endTime">
						<date-time-picker placeholder="请选择结束时间" :defaultValue="form.endTime" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" required label="出差时长" labelAlign="left" label-width="190">
						<view>{{ dateRangeSea }}小时</view>
					</u-form-item>

					<u-form-item class="form-item" required label="出差原因" labelAlign="left" label-width="190" prop="evectionReason">
						<u-input v-model="form.evectionReason" type="textarea" placeholder="请输入出差原因" :clearable="false" :height="160" :maxlength="-1" />
					</u-form-item>
					<u-form-item class="form-item" label="上传文件" labelAlign="left" label-width="190"><uni-file-upload width="500rpx"></uni-file-upload></u-form-item>
				</u-form>
			</view>

			<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">提交</u-button></view>
		</view>
		<view v-show="tabsCurrent == 1">
			<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#f5f7fa' }">
				<view class="list-info_04" v-for="(item, index) of infoList" :key="item.id" @click="navTo(item)">
					<view class="info-img"><image class="sub-img" :src="$imagePreview(item.logo)" mode="aspectFill"></image></view>
					<view class="info-btm">
						<view class="left-title clamp">{{ item.userName }}【出差申请】</view>
						<view class="left-time clamp">{{ item.userJob }} | {{ item.createTime | parseTime('{y}-{m}-{d}') }}</view>
					</view>
					<view class="right-type" :style="'color: ' + colorMap[item.approvalStatus]">{{ item.approvalStatusLabel }}</view>
				</view>
				<u-loadmore :status="loading" />
			</view>

			<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
		</view>

		<view v-if="false && evection"></view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import uniFileUpload from '@/components/uni-file-uploader.vue';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker,
		uniFileUpload
	},
	data() {
		return {
			id: '',
			tabsCurrent: 0,
			scrollTop: 0,

			tabsList: [
				{
					name: '发起提交',
					value: 0
				},
				{
					name: '查看数据',
					value: 1
				}
			],

			dept: '',

			colorMap: {
				0: '#303133',
				1: '#f29100',
				2: '#18b566',
				3: '#dd6161',
				4: '#0099ff',
				5: '#ff9900',
				6: '#19be6b',
				7: '#ff3333',
				8: '#909399'
			},

			type: '',
			typeShow: false,
			typeList: [],

			kind: '',
			kindShow: false,
			kindList: [],

			beginTime: '',
			endTime: '',

			form: {
				userId: null,
				deptId: null,
				userJob: null,
				vehicle: null,
				evectionType: null,
				beginTime: null,
				endTime: null,
				evectionDuration: null,
				evectionReason: null
			},
			rules: {
				title: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				userJob: [
					{
						required: true,
						message: '请选择职务',
						trigger: 'change'
					}
				],
				evectionType: [
					{
						required: true,
						message: '请选择单程往返',
						trigger: 'change'
					}
				],
				beginTime: [
					{
						required: true,
						message: '请选择开始时间',
						trigger: 'change'
					}
				],
				endTime: [
					{
						required: true,
						message: '请选择结束时间',
						trigger: 'change'
					}
				],
				evectionReason: [
					{
						required: true,
						message: '请选择出差原因',
						trigger: 'change'
					}
				]
			},
			status: 1,
			flag: '1',

			infoList: [],

			evection: {
				status: '1',
				userId: '',
				clientCode: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		this.loadData();
		this.loadDict('oa_attnd_vehicle', 'kindList');
		this.loadDict('oa_attnd_evection_type', 'typeList');

		if (option && option.tabs) {
			this.tabsCurrent = option.tabs;
		}

		if (option && option.id) {
			this.id = option.id;
			this.tabsCurrent = 0;
			this.spareData();
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore' && this.tabsCurrent == 1) this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		dateRangeSea: {
			get: function() {
				let time = this.endTime - this.beginTime;
				if (this.beginTime == '' || this.endTime == '') {
					return '0';
				} else if (this.endTime < this.beginTime) {
					this.$msg('开始时间应小于结束时间');
					return '0';
				} else {
					return (time / 3600).toFixed(1);
				}
			},
			set: function(e) {
				this.beginTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo', 'fileList'])
	},
	methods: {
		kindConfirm(e) {
			this.kind = e[0].label;
			this.form.vehicle = e[0].value;
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.form.evectionType = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.evection.clientCode = this.userInfo.sysUser.clientCode;
				this.evection.userId = this.userInfo.sysUser.id;
				this.form.clientCode = this.userInfo.sysUser.clientCode;
				this.form.userId = this.userInfo.sysUser.id;
				this.form.createBy = this.userInfo.sysUser.id;
				this.form.deptId = this.userInfo.sysUser.deptId;
				this.dept = this.userInfo.deptName;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		spareData() {
			this.$request('/oaAttndEvection/oa-attnd-evection/query/' + this.id).then(res => {
				this.$store.commit('filePush', res.data.fileList);
				this.form = res.data.oaAttndEvection;
				this.dept = this.form.deptName;

				let regx = /<[^>]*>|<\/[^>]*>/gm;
				if (this.form.evectionReason) {
					this.form.evectionReason = this.form.evectionReason.replace(regx, '');
				}

				this.beginTime = Math.round(Date.parse(this.form.beginTime) / 1000);
				this.endTime = Math.round(Date.parse(this.form.endTime) / 1000);

				this.typeList.forEach(item => {
					if (this.form.evectionType == item.value) {
						this.type = item.label;
					}
				});
				this.kindList.forEach(item => {
					if (this.form.vehicle == item.value) {
						this.kind = item.label;
					}
				});
			});
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			if (this.evection.status == '-1') {
				this.evection.status = '';
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				evection: this.evection
			};
			this.$request('/oaAttndEvection/oa-attnd-evection/list', data, {
				load: true
			}).then(res => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.infoList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.infoList = this.infoList.concat(res.data.records);
				this.loaded = true;
				if (this.infoList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then(res => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach(item => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			if (index == 1) {
				this.loadData();
			}
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute2 + ':' + '00';
			this[idx] = Math.round(dt / 1000);
		},
		spare() {
			this.flag = '0';
			this.confirm();
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pagesB/oa/detail_no3?id=' + item.id + '&undo=true'
			});
		},
		confirm() {
			this.$refs.uForm.validate(valid => {
				if (!valid) {
					return false;
				} else {
					let data = {};

					if (this.dateRangeSea <= 0) {
						this.$msg('请选择请假时长');
						return false;
					}
					this.form.evectionDuration = this.dateRangeSea;

					if (this.id) {
						this.form.id = this.id;
					}
					data = {
						evection: this.form,
						fileList: this.fileList
					};
					this.$request('/oaAttndEvection/oa-attnd-evection/add', data, {
						load: true
					}).then(res => {
						this.$msg(res.msg);
						setTimeout(() => {
							this.tabsCurrent = 1;
							this.loadData('refresh');
							uni.pageScrollTo({
								scrollTop: 0,
								duration: 0
							});
						}, 500);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.add-view {
	background-color: #fff;
}

.form-box {
	border-top: 20rpx solid #f5f7fa;

	.form-item {
		display: block;
		padding: 0 30rpx;
		border-bottom: 20rpx solid #f5f7fa;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}

.list-box {
	padding: 0;
	border-top: 20rpx solid #f5f7fa;
	border-bottom: 20rpx solid #f5f7fa;
}
</style>
